﻿
$(document).ready(function () {

    // $("input#txtDistance").numeric();
    // $("input#txtKgLoad").numeric();
    $("span#vldDistance").hide();
    $("span#vldTBKgLoad").hide();


    $("input#btnTBOK").click(function () {
        var vldOK = true;
        if (!validateAmount_60pt($("input#txtDistance"))) {
            $("span#vldDistance").show();
            vldOK = false;
        }
        else
            $("span#vldDistance").hide();

        if (!validateAmount_60pt($("input#txtKgLoad"))) {
            $("span#vldTBKgLoad").show();
            vldOK = false;
        }
        else
            $("span#vldTBKgLoad").hide();


        if (vldOK == false)
            return;

        processTB();
        $.fancybox.close();
    });

    $("input#btnTBCancel").click(function () {
        $.fancybox.close();
    });
});

function resetTBModal() {
    $("select#ddlVehicle option:eq(0)").prop('selected', 'selected');

    $("input#txtDistance").val('');
    // $("select#ddlDistanceUnit option:eq(0)").prop('selected', 'selected');
    $("select#ddlDistanceUnit").val('0440a2e0-ad6a-4edb-99b4-c6cfbfa12409');
    $("input#txtKgLoad").val('');
    // $("select#ddlKgLoadUnit option:eq(0)").prop('selected', 'selected');
    $("select#ddlKgLoadUnit").val('5f10554d-8e05-4845-86f1-fb858b6fbfb9');
}

function loadTBModal(selectedRow) {
    $("select#ddlVehicle").val(selectedRow.vehicle_id);
    $("input#txtDistance").val(selectedRow.distance);
    $("select#ddlDistanceUnit").val(selectedRow.unit_distance_id);
    $("input#txtKgLoad").val(selectedRow.kg_load);
    $("select#ddlKgLoadUnit").val(selectedRow.unit_kgload_id);
}

function processTB() {
    var vehicle_id = $("select#ddlVehicle").val();
    var vehicle_name = $.trim($("select#ddlVehicle option:selected").text());


    var distance = $("input#txtDistance").val();
    var unit_distance_id = $("select#ddlDistanceUnit").val();
    var unit_distance_name = $("select#ddlDistanceUnit option:selected").text();
    unit_distance_name = $.trim(unit_distance_name.split('-')[1]);

    var kg_load = $("input#txtKgLoad").val();
    var unit_kgload_id = $("select#ddlKgLoadUnit").val();
    var unit_kgload_name = $("select#ddlKgLoadUnit option:selected").text();
    unit_kgload_name = $.trim(unit_kgload_name.split('-')[1]);

    var row = {
        vehicle_id: vehicle_id,
        vehicle_name: vehicle_name,
        distance: distance,
        unit_distance_id: unit_distance_id,
        unit_distance_name: unit_distance_name,
        kg_load: kg_load,
        unit_kgload_id: unit_kgload_id,
        unit_kgload_name: unit_kgload_name
    };

    callbackFromTBModal(row);
}